require"common"

--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )
        for i=1,len do
                c = string.sub(num,i,i)
                if tonumber(c) > max or tonumber(c) <0 then return nil end
        end

        return true
end



--返回注数,倍投数 , 投注串, 指令串
function random_j4( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  602#注码#B倍数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = selectNumber(12,3,1,0)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]..nums[4]..nums[5]..nums[6]..nums[7]..nums[8]..nums[9]..nums[10]..nums[11]..nums[12]
			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_j4('1', zlStr)
	end

	return butNum, butPower, butStr, zlStr


end

--返回注数,倍投数,投注串
function check_j4( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 601#注码#B倍数  1401#310301001110#010301001110
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 8 then return 0 end
			if not judge_str(xItem[i],3) then return 0 end
		end
	--复式自选 603#A#B#C#D#E#F#G#H#I#J#K#L
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 8 then return 0 end
		butNum = 1
		for i=1,#xItem do
			if string.len(xItem[i]) > 4 then return 0 end
			if not judge_str(xItem[i],3)  then return 0 end
			butNum = butNum*string.len(xItem[i])
		end
	else
		return 0
	end;
	return butNum,butPower,inStr,inStr
end

---========================中奖查询=================================================================
function query_j4(butType,queryStr,djStr)
	local butPower = 1
	local itemList = {}
	local result = 0
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	if  butType == '1' then
		jItem = Split(itemList[1], "#")
		result =MultSimpCompute(jItem,djStr,butPower,8,1,butType)

	elseif butType == '2' then
		jItem = Split(itemList[1], "#")
		result = MultSimpCompute(jItem,djStr,butPower,8,1,butType)

	else
		return 0
	end;

	return result
end


--===================================================================================================
--print(random_j4('1',"5#B11"))
--print(check_j4('1',"30102011#31222000#B10"))
--print(check_j4('2',"3120#310#031#0#0#0#0#0#B31"))
--print(query_j4('1',"30102011#31222000#B99","30102011"))
--print(query_j4('2',"310#310#031#0#2#0#1#1#B31","30102011"))
